package com.pucheng.bracelet.app.service;

import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.text.TextUtils;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.clj.fastble.BleManager;
import com.clj.fastble.callback.BleWriteCallback;
import com.clj.fastble.exception.BleException;
import com.pucheng.bracelet.app.ble.BleUUId;
import com.pucheng.bracelet.app.ble.BleWriteAndNotifyCallback;
import com.pucheng.bracelet.app.ble.ClientManager;
import com.pucheng.bracelet.app.util.ByteUtils;
import com.pucheng.bracelet.app.util.CacheUtil;
import com.pucheng.bracelet.app.util.WriteUtils;
import com.pucheng.bracelet.ui.adapter.PushData;
import com.pucheng.bracelet.viewmodel.meaasge.Push;

import java.util.List;


/**
 * 消息监听服务
 */
public class NotificationCollectorService extends NotificationListenerService {
    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        // 微信包名：com.tencent.mm
        // 系统电话：com.android.incallui
        LogUtils.e("应用包名：" + sbn.getPackageName());
        String title = null;
        Object titleObj =sbn.getNotification().extras.get("android.title");
        if (titleObj != null) {
            if (titleObj instanceof String) {
                title = (String) titleObj;
            }else {
                title = titleObj.toString();
            }
        }
        String text = null;
        Object textObj = sbn.getNotification().extras.get("android.text");
        if (textObj != null) {
            if (textObj instanceof String) {
                text = (String)  textObj;
            }else {
                text = textObj.toString();
            }
        }


        LogUtils.e("消息标题：" + title);
        LogUtils.e("消息内容：" + text);
        if (title != null && text != null) {
            String mac = CacheUtil.INSTANCE.getWatchInfo().getMac();
            if (!TextUtils.isEmpty(mac)) {
                if (!TextUtils.isEmpty(mac) && BleManager.getInstance().isConnected(mac)) {
                    PushData pushData = checkApp(sbn.getPackageName());
                    if (pushData != null && pushData.getSelected()) {
        //                String title = (String) sbn.getNotification().extras.get("android.title");
        //                Stringng text = sbn.getNotification().extras.get("android.text").toString();
                        if (!StringUtils.isEmpty(title) || !StringUtils.isEmpty(text)) {
                            sendMsg(pushData.getId(), title + "\n" + text);
                        }

                    }
                }
            }
        }

    }

    private void sendMsg(int id, String sendMsgStr) {
        byte[] makeChecksumByte = new byte[sendMsgStr.getBytes().length + 1];
        makeChecksumByte[0] = ByteUtils.intToByte(id);
        System.arraycopy(sendMsgStr.getBytes(), 0, makeChecksumByte, 1, sendMsgStr.getBytes().length);
        String makeChecksum = ByteUtils.makeChecksum(makeChecksumByte);
        String length = ByteUtils.decToHex(sendMsgStr.getBytes().length+1);
        if (length.length() < 4){
            length += "00";
        }
        LogUtils.e("makeChecksum: " + makeChecksum);
        if (makeChecksum.length() == 2) {
            makeChecksum = "00" + makeChecksum;
        }
        byte[] sendByte = new byte[sendMsgStr.getBytes().length + 2];
        sendByte[0] = (byte) 0xDA;
        sendByte[1] = ByteUtils.intToByte(id);
        System.arraycopy(sendMsgStr.getBytes(), 0, sendByte, 2, sendMsgStr.getBytes().length);
        ClientManager.getInstance().sendMsg(WriteUtils.getWriteBytes("FB" + length + makeChecksum),sendByte, new BleWriteAndNotifyCallback() {
                    @Override
                    public void onCharacteristicChanged(byte[] data) {

//                        ClientManager.getInstance().wirteCmd(BleUUId.UUID_3805, BleUUId.UUID_5A01,"DA0150505050505050505050505050505050505050505050505050505050505050",  new BleWriteAndNotifyCallback() {
//                            @Override
//                            public void onCharacteristicChanged(byte[] data) {
//
//                            }

                        ClientManager.getInstance().sendData(sendByte, BleUUId.UUID_3805, BleUUId.UUID_5A01, new BleWriteCallback() {
                            @Override
                            public void onWriteSuccess(int current, int total, byte[] justWrite) {
                                LogUtils.e("通知推送成功：" + justWrite);
                            }

                            @Override
                            public void onWriteFailure(BleException exception) {
                                LogUtils.e("通知推送失败：" + exception.toString());
                            }
                        });
                    }

                    @Override
                    public void onWriteSuccess(int current, int total, byte[] justWrite) {

                    }

                    @Override
                    public void onWriteFailure(BleException exception) {

                    }
                });

    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
//        LogUtils.i( "remove" + "-----" + sbn.getPackageName());

    }

    private PushData checkApp(String packageName) {
        Push pushMsg = CacheUtil.INSTANCE.getPushMsgState();

        List<PushData> data = pushMsg.getData();
        if (data.size() > 0) {
            switch (packageName) {
                case "com.android.incallui"://电话
                    return data.get(0);
                case "com.android.mms"://短信
                    return data.get(1);
                case "com.tencent.mobileqq"://QQ
                    return data.get(2);
                case "com.tencent.mm"://微信
                    return data.get(3);
                case "com.facebook.katana"://facebook
                    return data.get(5);
                case "com.twitter.android"://Twitter
                    return data.get(6);
                case "com.whatsapp"://whatsapp
                    return data.get(7);
                case "com.skype.raider"://skype
                    return data.get(9);
                case "com.instagram.android"://instagram
                    return data.get(10);
                case "jp.naver.line.android"://Line
                    return data.get(11);
                default:
                    return data.get(4);
            }
        }
        return new PushData(4,"",0,true);
    }
}
